package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.math.NumberUtils;

import com.sun.media.sound.ModelSource;

import models.Friend;
import models.Journey;
import models.Point;
import models.JourneyWall;
import models.PointWall;
import models.Updates;
import models.User;
import models.Wall;
import play.db.jpa.Model;
import play.mvc.Controller;
import play.mvc.With;
import services.JourneyService;
import services.PointService;
import services.ProfileService;
import services.UpdatesService;
import services.UserService;

@With(Secure.class)
public class Profile extends Controller{
    @Inject
    private static ProfileService profileService;
    @Inject
    private static UserService userService;
    @Inject
    private static JourneyService journeyService;
    @Inject
    private static PointService pointService;
    @Inject
    private static UpdatesService updatesService;
    
    public static void index(int user_id) {
        List<Updates> posts;
        User user;
        if(user_id > 0){
            user = userService.getUserByUserId(user_id);
            posts =  profileService.getPostsByUserId(user_id);
        } else {
            String username = Secure.Security.connected();
            user = userService.getUserByUsername(username);
            posts =  profileService.getPosts(username);
        }
        Wall wall = new Wall();
        wall.setUser(user);
        wall.setUpdates(posts);
        render(wall);
    }
    
    public static void friends(int user_id){
        List<User> friends;
        User user;
        if(user_id > 0){
            user = userService.getUserByUserId(user_id);
            friends = userService.getFriendsByUserId(user_id);
        } else {
            String username = Secure.Security.connected();
            user = userService.getUserByUsername(username);
            friends =  userService.getFriendsByUsername(username);
        }
        Friend friend = new Friend();
        friend.setUser(user);
        friend.setFriends(friends);
        render(friend);
    }
    
    public static void journey(int user_id){
        List<Journey> journeys;
        User user;
        if(user_id > 0){
            user = userService.getUserByUserId(user_id);
            journeys = journeyService.getJourneysByUserId(user_id);
        } else {
            String username = Secure.Security.connected();
            user = userService.getUserByUsername(username);
            journeys =  journeyService.getJourneysByUsername(username);
        }
        JourneyWall data = new JourneyWall();
        data.setUser(user);
        data.setJourneys(journeys);
        render(data);
    }
    public static void sharejourney(int user_id,int journey_id,int share_id){
    	List<Journey> journeys;
        User user;
            user = userService.getUserByUserId(user_id);
            journeys = journeyService.getShareJourneysByUserId(journey_id);
            Updates updates = new Updates();
            updates.setJourney_id(journey_id);
            //updates.setUser_id(user_id);
            updatesService.updateVisited(share_id);
        JourneyWall data = new JourneyWall();
        data.setUser(user);
        data.setJourneys(journeys);
        render(data);
    }
    public static void shareposition(int user_id,int position_id,int share_id){
    	List<Point> points;
        User user;

            user = userService.getUserByUserId(user_id);
            points = pointService.getSharePointsByUserId(position_id);
            Updates updates = new Updates();
            updates.setPosition_id(position_id);
            //updates.setUser_id(user_id);
            updatesService.updateVisited(share_id);
        PointWall data = new PointWall();
        data.setUser(user);
        data.setPoints(points);
        render(data);
    }
    public static void point(int user_id){
        List<Point> points;
        User user;
        if(user_id > 0){
            user = userService.getUserByUserId(user_id);
            points = pointService.getPointsByUserId(user_id);
        } else {
            String username = Secure.Security.connected();
            user = userService.getUserByUsername(username);
            points =  pointService.getPointsByUsername(username);
        }
        PointWall data = new PointWall();
        data.setUser(user);
        data.setPoints(points);
        render(data);
    }
    public static void get_friend() {
     List<User> friends;	
     String username = Secure.Security.connected();
	 friends =  userService.getFriendsByUsername(username);
	 Friend friend = new Friend();
	 friend.setFriends(friends);
	 renderJSON(friend);
	}
    public static void search_friend() {
    	String num = params.get("number");
    	int number  = (NumberUtils.isNumber(num)) ? Integer.valueOf(num) : 0;
        List<User> friends;	
        String username = Secure.Security.connected();
        models.User fri = new User();
        fri = userService.getUserByUsername(username);
        //fri.getUser_id();
        fri.setNumber(number);
        //fri.setUsername(username);
   	 friends =  userService.getFriendsByNumber(fri);
   	 Friend friend = new Friend();
   	 friend.setFriends(friends);
   	 renderJSON(friend);
   	}
    public static void addfriend() {
    	String user_sh_id = params.get("user_id");
    	String jour_id = params.get("journey_id");
    	Updates update = new Updates();
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	update.setUsername(Secure.Security.connected());
    	//update.setNotices(notices);
    	int journey_id  = (NumberUtils.isNumber(jour_id)) ? Integer.valueOf(jour_id) : 0;
    	int user_shared_id  = (NumberUtils.isNumber(user_sh_id)) ? Integer.valueOf(user_sh_id) : 0;
    	update.setJourney_id(journey_id);
    	update.setUser_shared_id(user_shared_id);
    	User user = userService.getUserByUsername(Secure.Security.connected());
    	update.setUser_id(user.getId().intValue());
    	update.setStr_date(sdf.format(date));
    	//update.getUser_id();
    	updatesService.addfriend(update);
    	//render();
	}
}